//
//  AppDelegate.m
//  BaseProject
//
//  Created by 王洋 on 2020/3/19.
//  Copyright © 2020 王洋. All rights reserved.
//

#import "AppDelegate.h"
#import "AppVersionModel.h"

#import <AlipaySDK/AlipaySDK.h>
#import <SDImageWebPCoder.h>
#import <AppTrackingTransparency/AppTrackingTransparency.h>

#import "TT_IMManager.h"
#import "TT_GDMapManager.h"

#import "TT_WebviewController.h"
#import "TT_TabbarViewController.h"

#import <ShareSDK/ShareSDK.h>

#import "BA_LeadViewController.h"
#import "OnboardingViewController.h"
#import "OnboardingContentViewController.h"

// 银联支付
#import "UMSPPPayPluginSettings.h"
#import "UMSPPPayVersionsInfo.h"
#import "UMSPPPayUnifyPayPlugin.h"

#import "AppDelegate+OpenInstall.h"
#import "AppDelegate+JiGuang.h"
#import "AppDelegate+Update.h"
#import "AppDelegate+WeChat.h"
#import "AppDelegate+Alipay.h"
#import "AppDelegate+BaMarryCall.h"
#import <LKLPaymentSdk/LKLUnionCashier.h>
@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [UMSPPPayPluginSettings sharedInstance].umspEnviroment = UMSP_TEST;
    [self initSVProgressStyle];
    [self initOpenInstall];
    [self initJiGuangWithOption:launchOptions];
    [self initWeChat];
    [self initThirdSDKWithOptions:launchOptions];
    //return [self mockTest];
    [self initRoot];
    [self versionUpdate];
    [[BA_BusinessHelper shared] syncBusinessData];
    [StatisticManager shareManager];
    return YES;
}
- (BOOL)mockTest {
    TESTVC *testVc = [[TESTVC alloc]init];
    self.window.rootViewController = testVc;
    return YES;
}
- (void)initSVProgressStyle {
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];
    if (@available(iOS 14, tvOS 14, macOS 11, watchOS 7, *)) {
        // iOS 14 supports WebP built-in
        [[SDImageCodersManager sharedManager] addCoder:[SDImageAWebPCoder sharedCoder]];
    } else {
        // iOS 13 does not supports WebP, use third-party codec
        [[SDImageCodersManager sharedManager] addCoder:[SDImageWebPCoder sharedCoder]];
    }
    [IQKeyboardManager sharedManager].enable = YES;
}
- (void)initThirdSDKWithOptions:(NSDictionary *)launchOptions {
    // share
    [ShareSDK registPlatforms:^(SSDKRegister *platformsRegister) {
        //QQ
        [platformsRegister setupQQWithAppId:S.appKey_qq appkey:S.secret_qq enableUniversalLink:YES universalLink:S.universalLink];
        //weixin
        [platformsRegister setupWeChatWithAppId:S.appKey_weixin appSecret:S.secret_weixin universalLink:S.universalLink];
        //新浪
        [platformsRegister setupSinaWeiboWithAppkey:S.appKey_weibo appSecret:S.secret_weibo redirectUrl:S.redirectUrl universalLink:S.universalLink];
    }];
    // IM
    [[TT_IMManager sharedInstance] TTIM_InitSDK];
    if ([TTUserModel sharedObject].identifier.length > 0 && [TTUserModel sharedObject].userSig > 0) {
        [[TT_IMManager sharedInstance] BA_IMLogin];
    }
    // Map
    [[TT_GDMapManager shareMap] initMapSDK];
}
- (void)initRoot {
    //检查是否启动引导页
    NSString *start_guide_status = [Utilities getFromNSUserDefaults:@"start_guide_status"];
    if (![start_guide_status isEqualToString:@"1"]) {
        self.window.rootViewController = [self generateStandardOnboardingVC];
    } else {
        [self setupNormalRootViewController];
    }
}
- (void)setupNormalRootViewController {
    TT_TabbarViewController *tabBarController = [TT_TabbarViewController shareTabbar];
    //tabBarController.selectedIndex = 4;
    self.window.rootViewController = tabBarController;
}
- (OnboardingViewController *)generateStandardOnboardingVC {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.window makeKeyAndVisible];
    self.window.backgroundColor = [UIColor whiteColor];
    // welcomePage loadpage4
    BA_LeadViewController *forthPage = [BA_LeadViewController contentWithTitle:@"" body:@"" image:[UIImage imageNamed:@"welcomePage"] buttonText:@"" action:^{
        [self handleOnboardingCompletion];
        [Utilities setToNSUserDefaults:@"start_guide_status" value:@"1"];
    }];
    OnboardingViewController *onboardingVC = [OnboardingViewController onboardWithBackgroundImage:[UIImage imageNamed:@"street"] contents:@[forthPage, forthPage]];
    onboardingVC.shouldFadeTransitions = YES;
    onboardingVC.shouldMaskBackground = NO;
    onboardingVC.fadePageControlOnLastPage = YES;
    onboardingVC.fadeSkipButtonOnLastPage = YES;
    //onboardingVC.allowSkipping = YES;
    onboardingVC.pageControl.pageIndicatorTintColor = UIColor.clearColor;
    onboardingVC.pageControl.currentPageIndicatorTintColor = UIColor.clearColor;
    onboardingVC.pageControl.hidden = YES;
    onboardingVC.skipHandler = ^{
        [self handleOnboardingCompletion];
        [Utilities setToNSUserDefaults:@"start_guide_status" value:@"1"];
    };
    forthPage.topPadding = 0;
    forthPage.bottomPadding = 0;
    forthPage.iconWidth = TTScreenWidth;
    forthPage.iconHeight = TTScreenHeight;
    return onboardingVC;
}
- (void)handleOnboardingCompletion {
    [self setupNormalRootViewController];
}

- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray<id<UIUserActivityRestoring>> * _Nullable))restorationHandler {
    NSLog(@"application:continueUserActivity:restorationHandler:");
    // 处理通过openinstall一键唤起App时传递的数据
    [self application_appInstall:application continueUserActivity:userActivity restorationHandler:restorationHandler];
    return [self application_wechat:application continueUserActivity:userActivity restorationHandler:restorationHandler];
    return YES;
}
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
    NSLog(@"application:openURL:%@ %@ %@", url, url.scheme, url.host);
    // 拉卡拉
    [LKLUnionCashier handlePaymentResult:url];
    // appInstall
    [self application_appInstall:app openURL:url options:options];
    if ([self isBaMarryCall:url]) {
        return [self application_baMarryCall:app openURL:url options:options];
    }
    if ([self isAlipayHost:url]) {
        return [self application_alipay:app openURL:url options:options];
    }
    if ([self isWeChatScheme:url]) {
        return [self application_wechat:app openURL:url options:options];
    }
    return YES;
}



- (void)applicationDidEnterBackground:(UIApplication *)application {
    //doBackground
    TIMBackgroundParam  *param = [[TIMBackgroundParam alloc] init];
    [[TIMManager sharedInstance] doBackground:param succ:^() {
        NSLog(@"doBackgroud Succ");
    } fail:^(int code, NSString * err) {
        NSLog(@"Fail: %d->%@", code, err);
    }];
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    if (@available(iOS 14.0, *)) {
        [ATTrackingManager requestTrackingAuthorizationWithCompletionHandler:^(ATTrackingManagerAuthorizationStatus status) {
            
        }];
    }
    [[TIMManager sharedInstance] doForeground:^() {
        NSLog(@"doForegroud Succ");
    } fail:^(int code, NSString * err) {
        NSLog(@"Fail: %d->%@", code, err);
    }];
}
@end
